//
//  PresentationController.swift
//  CBWWeiBo
//
//  Created by 陈博文 on 16/1/8.
//  Copyright © 2016年 陈博文. All rights reserved.
//

import UIKit

class PresentationController: UIPresentationController {
    
    // MARK:- 属性
    var presentedFrame = CGRectZero


    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    
    
    // MARK:- 系统回调的函数
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        
        
        // 1.改变弹出控制器的View的尺寸
        presentedView()?.frame = presentedFrame
        
        // 2.添加一个蒙版,点击蒙版时,让控制器消失
        setupCoverView()
    }
    
    
    // MARK:- 自定义的函数
    private func setupCoverView() {
        // 1.创建蒙版,并且设置属性
        let coverView = UIView()
        coverView.frame = containerView!.bounds
        coverView.backgroundColor = UIColor(colorLiteralRed: 0.1, green: 0.1, blue: 0.1, alpha: 0.1)
        
        // 2.添加手势
        let tap = UITapGestureRecognizer(target: self, action: "coverViewClick")
        coverView.addGestureRecognizer(tap)
        
        // 3.添加到容器视图中
        containerView?.insertSubview(coverView, belowSubview: presentedView()!)
    }
    
    // MARK:- 事件监听函数
    @objc private func coverViewClick() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }

}
